package cn.wh.xwm.controller.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Transaction;

import cn.wh.xwm.common.util.Constant;
import cn.wh.xwm.controller.action.vo.UserInfoView;
import cn.wh.xwm.controller.action.vo.CustomerView;
import cn.wh.xwm.hibernate.dao.SalesLiquid;
import cn.wh.xwm.hibernate.dao.Userinfo;
import cn.wh.xwm.hibernate.dao.UserinfoDAO;

/**
 * 用户管理 ACTION
 * @author swimming
 *
 */
public class UserAction extends BaseAction {
	
	private String username;
	private String password;
	private String sex;
	private String tel;
	private String role;
	private String userid;
	private String page;
	private String rows;
	
	/**
	 * 查询条件
	 */
	private String q_username;
	private String q_tel;
	
	public String init(){
		return LOGIN;
	}
	
	/**
	 * 增加用户
	 * @return
	 * @throws IOException 
	 */
	public void addUser() throws IOException{
		UserinfoDAO dao = new UserinfoDAO();
		Userinfo userinfo = new Userinfo();
		userinfo.setUsername(username);
		userinfo.setPassword(password);
		userinfo.setSex(sex);
		userinfo.setRole(role.equals("PM")?1:2);
		userinfo.setTel(tel);
		Transaction ts = dao.getSession().getTransaction();
		ts.begin();
		dao.save(userinfo);
		ts.commit();
		JSONObject object = new JSONObject();
		object.put("success", true);
		dao.getSession().close();
		this.responseJson(object);
	}
	
	/**
	 * 修改用户
	 * @throws IOException 
	 */
	public void updateUser() throws IOException{
		UserinfoDAO dao = new UserinfoDAO();
		Userinfo userinfo = dao.findById(userid);
		userinfo.setUsername(username);
		userinfo.setPassword(password);
		userinfo.setSex(sex);
		userinfo.setRole(role.equals("PM")?1:2);
		userinfo.setTel(tel);
		Transaction ts = dao.getSession().getTransaction();
		ts.begin();
		dao.update(userinfo);
		ts.commit();
		JSONObject object = new JSONObject();
		object.put("success", true);
		dao.getSession().close();
		this.responseJson(object);
	}
	
	/**
	 *删除用户
	 * @throws IOException 
	 */
	public void deleteUser() throws IOException{
		UserinfoDAO dao = new UserinfoDAO();
		Userinfo userinfo = dao.findById(userid);
		Transaction ts = dao.getSession().getTransaction();
		ts.begin();
		dao.delete(userinfo);
		ts.commit();
		JSONObject object = new JSONObject();
		object.put("success", true);
		dao.getSession().close();
		this.responseJson(object);
	}

	/**
	 * 查询用户
	 * @return
	 * @throws IOException 
	 */
	public void findUser() throws IOException{
		UserinfoDAO dao = new UserinfoDAO();
		List<Userinfo> list = dao.findBypage(page,rows,"Userinfo");
		List<UserInfoView> vlist = new ArrayList<UserInfoView>();
		for(Userinfo info : list){
			UserInfoView view = new UserInfoView();
			view.setUsername(info.getUsername());
			view.setPassword(info.getPassword());
			if(info.getRole()==0){
				view.setRole("超级管理员");
			}else{
				view.setRole(info.getRole()==1?"PM":"SALES");
			}
			view.setTel(info.getTel());
			view.setSex(info.getSex());
			view.setId(info.getUserid());
			vlist.add(view);
		}
		JSONObject object = new JSONObject();
		object.put("rows", vlist);
		object.put("total", dao.findTotalCount("Userinfo"));
		dao.getSession().close();
		this.responseJson(object);
	}

	/**
	 * 条件查询
	 * @throws IOException
	 */
	public void findByParam() throws IOException{
		UserinfoDAO dao = new UserinfoDAO();
		List<Userinfo> list = dao.findByPrame(page,rows,q_tel,q_username);
		List<UserInfoView> vlist = new ArrayList<UserInfoView>();
		for(Userinfo info : list){
			UserInfoView view = new UserInfoView();
			view.setUsername(info.getUsername());
			view.setPassword(info.getPassword());
			view.setRole(info.getRole()==1?"PM":"SALES");
			view.setTel(info.getTel());
			view.setSex(info.getSex());
			view.setId(info.getUserid());
			vlist.add(view);
		}
		JSONObject object = new JSONObject();
		object.put("rows", vlist);
		object.put("total", dao.findTotalBysql(q_tel,q_username));
		dao.getSession().close();
		this.responseJson(object);
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}


	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getQ_username() {
		return q_username;
	}

	public void setQ_username(String q_username) {
		this.q_username = q_username;
	}

	public String getQ_tel() {
		return q_tel;
	}

	public void setQ_tel(String q_tel) {
		this.q_tel = q_tel;
	}

}
